<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="客户id" prop="customerInfoId">
        <el-input
          v-model="queryParams.customerInfoId"
          placeholder="请输入客户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="商品名称" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="服务机构" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入服务机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务人员" prop="serviceStaff">
        <el-input
          v-model="queryParams.serviceStaff"
          placeholder="请输入服务人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客服人员" prop="customerService">
        <el-input
          v-model="queryParams.customerService"
          placeholder="请输入客服人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择服务状态" clearable>
          <el-option
            v-for="dict in dict.type.service_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="套餐总次数" prop="packageCounts">
        <el-input
          v-model="queryParams.packageCounts"
          placeholder="请输入套餐总次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已核销次数" prop="verifiedTimes">
        <el-input
          v-model="queryParams.verifiedTimes"
          placeholder="请输入已核销次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余次数" prop="surplusTimes">
        <el-input
          v-model="queryParams.surplusTimes"
          placeholder="请输入剩余次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:serviceplan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:serviceplan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:serviceplan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:serviceplan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceplanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" width="55" align="center" prop="servicePlanId" />
      <el-table-column label="openid" width="150" align="center" prop="openid" />
      <el-table-column label="订单流水"  width="150" align="center" prop="orderFlow" />
      <!--<el-table-column label="客户id" align="center" prop="customerInfoId" />-->
      <el-table-column label="客户姓名" width="80" align="center" prop="customerName" />
     <!-- <el-table-column label="商品id" align="center" prop="packageInfoId" />-->
      <el-table-column label="套餐名称" width="160" align="center" prop="packageName" />

      <el-table-column label="服务状态" width="100" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="服务机构" width="100" align="center" prop="deptId" />
      <el-table-column label="服务人员" align="center" prop="serviceStaff" />
      <el-table-column label="客服人员" align="center" prop="customerService">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ck_customer_service" :value="scope.row.customerService"/>
        </template>
      </el-table-column>
      <el-table-column label="服务阶段" width="100" align="center" prop="servicePhase" />
     <!-- <el-table-column label="服务阶段" align="center" prop="servicePhase">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.servicePhase" :value="scope.row.servicePhase ? scope.row.servicePhase.split(',') : []"/>
        </template>
      </el-table-column>-->



      <el-table-column label="套餐总次数" align="center" prop="packageCounts" />
      <el-table-column label="已核销次数" align="center" prop="verifiedTimes" />
      <el-table-column label="剩余次数" align="center" prop="surplusTimes" />
      <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:serviceplan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:serviceplan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="客户id" prop="customerInfoId">
        <el-input v-model="form.customerInfoId" placeholder="请输入客户id" />
      </el-form-item>-->

      <el-form-item label="openid" prop="openid">
        <el-input v-model="form.openid" placeholder="请输入openid" />
      </el-form-item>

      <el-form-item label="订单流水" prop="orderFlow">
        <el-input v-model="form.orderFlow" placeholder="请输入订单流水" />
      </el-form-item>

        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
        </el-form-item>

        <el-form-item label="商品id" prop="packageInfoId">
          <el-input v-model="form.packageInfoId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="套餐名称" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="服务机构" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入服务机构" />
        </el-form-item>
        <!--<el-form-item label="服务阶段">
          <el-checkbox-group v-model="form.servicePhase">
            <el-checkbox
              v-for="dict in dict.type.service_phase"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>-->
        <el-form-item label="服务人员" prop="serviceStaff">
          <el-input v-model="form.serviceStaff" placeholder="请输入服务人员" />
        </el-form-item>
        <el-form-item label="客服人员" prop="customerService">
          <el-input v-model="form.customerService" placeholder="请输入客服人员" />
        </el-form-item>
        <el-form-item label="服务状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.service_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="套餐总次数" prop="packageCounts">
          <el-input v-model="form.packageCounts" placeholder="请输入套餐总次数" />
        </el-form-item>
        <el-form-item label="已核销次数" prop="verifiedTimes">
          <el-input v-model="form.verifiedTimes" placeholder="请输入已核销次数" />
        </el-form-item>
        <el-form-item label="剩余次数" prop="surplusTimes">
          <el-input v-model="form.surplusTimes" placeholder="请输入剩余次数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listServiceplan, getServiceplan, delServiceplan, addServiceplan, updateServiceplan } from "@/api/system/serviceplan";

  export default {
    name: "Serviceplan",
    dicts: ['service_status', 'service_phase'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 服务计划表格数据
        serviceplanList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          openid: null,
          orderFlow: null,
         /* customerInfoId: null,*/
          customerName: null,
          packageInfoId: null,
          packageName: null,
          deptId: null,
          servicePhase: null,
          serviceStaff: null,
          customerService: null,
          status: null,
          packageCounts: null,
          verifiedTimes: null,
          surplusTimes: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          customerName: [
            { required: true, message: "客户名称不能为空", trigger: "blur" }
          ],
          packageName: [
            { required: true, message: "商品名称不能为空", trigger: "blur" }
          ],
          servicePhase: [
            { required: true, message: "服务阶段不能为空", trigger: "blur" }
          ],
          packageCounts: [
            { required: true, message: "套餐总次数不能为空", trigger: "blur" }
          ],
          verifiedTimes: [
            { required: true, message: "已核销次数不能为空", trigger: "blur" }
          ],
          surplusTimes: [
            { required: true, message: "剩余次数不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询服务计划列表 */
      getList() {
        this.loading = true;
        listServiceplan(this.queryParams).then(response => {
          this.serviceplanList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          servicePlanId: null,
         /* customerInfoId: null,*/
          openid: null,
          orderFlow: null,
          packageInfoId: null,
          customerName: null,
          packageName: null,
          deptId: null,
          servicePhase: [],
          serviceStaff: null,
          customerService: null,
          status: "0",
          packageCounts: null,
          verifiedTimes: null,
          surplusTimes: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.servicePlanId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加服务计划";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const servicePlanId = row.servicePlanId || this.ids
        getServiceplan(servicePlanId).then(response => {
          this.form = response.data;
          this.form.servicePhase = this.form.servicePhase.split(",");
          this.open = true;
          this.title = "修改服务计划";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.servicePhase = this.form.servicePhase.join(",");
            if (this.form.servicePlanId != null) {
              updateServiceplan(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addServiceplan(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const servicePlanIds = row.servicePlanId || this.ids;
        this.$modal.confirm('是否确认删除服务计划编号为"' + servicePlanIds + '"的数据项？').then(function() {
          return delServiceplan(servicePlanIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/serviceplan/export', {
          ...this.queryParams
        }, `serviceplan_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
